package top.wettis.chat.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.wettis.chat.constant.KeyWordsConstant;
import top.wettis.chat.constant.ResultType;
import top.wettis.chat.constant.SubjectConstant;
import top.wettis.chat.mapper.HistoryRecordMapper;
import top.wettis.chat.pojo.DO.HistoryRecord;
import top.wettis.chat.pojo.DO.Task;
import top.wettis.chat.pojo.VO.Result;
import top.wettis.chat.service.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户提问历史记录 服务实现类
 * </p>
 *
 * @author wettis
 * @since 2023-03-25
 */
@Service
public class HistoryRecordServiceImpl extends ServiceImpl<HistoryRecordMapper, HistoryRecord> implements IHistoryRecordService {

    @Autowired
    private ITaskService taskService;
    @Autowired
    private IObjectivesService iObjectivesService;

    @Autowired
    private IQuestiionService iQuestiionService;

    @Autowired
    private IUserService iUserService;


    @Override
    @Transactional
    public void insert(String question, String ans, int userId) {
        HistoryRecord historyRecord = new HistoryRecord();
        historyRecord.setAnswer(ans);
        historyRecord.setDel(0);
        historyRecord.setQuestion(question);
        historyRecord.setUserId(userId);
        historyRecord.setQueryTime(LocalDate.now());
        save(historyRecord);
    }


    public Result<Object> ask(String question) {
        int id = StpUtil.getLoginIdAsInt();

        if(question.indexOf("计划") != -1){


            //TODO 包装问题
            return Result.customInfo(200,"包装问题" ,"未开发");
        }

        try {
            //TODO 从AI获取问题的答案
            String ans = "获取的答案";
            HistoryRecord historyRecord = new HistoryRecord(){{
               setDel(0);
               setUserId(id);
               setAnswer(ans);
               setQuestion(question);
               setQueryTime(LocalDate.now());
            }};
            save(historyRecord);
            return Result.customInfo(200,"其他问题" ,ans);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Result<Object> question(String question) {
        char week = ' ';
        Integer level = 0, order = 0;

        int user_id = StpUtil.getLoginIdAsInt();

        String word = new String();
        String q = new String();
        Map<String, Integer> cout = new HashMap<>();

        //询问次数+1
        iUserService.askjj(user_id);

        //包含关键字  前端/后端/ui
        for (SubjectConstant s : SubjectConstant.values()) {
            if (question.indexOf(s.getString()) != -1) {
                word = s.toString();
                cout.put(word, 1);
            }
        }

        //
        if (cout.size() == 1) {


            for (KeyWordsConstant s : KeyWordsConstant.values()) {

                if (question.indexOf(s.toString()) != -1) {

                    q = s.toString();

                    //获取答案
                    String ans = iQuestiionService.get(word, q);

                    //插入历史的问题
                    this.insert(question, ans, user_id);

                    if (q.equals("计划")) {

                        List<Task> select = taskService.select(user_id);
                        if (select.size() != 0) {
                            String mes = "您已有计划,请删除后在制定";
                            return Result.failInfo(ResultType.SAVE, mes);
                        }


                        LocalDateTime now = LocalDateTime.now();
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                        String dateTimeString = now.format(formatter);


                        // 更新user表
                        iUserService.addPlan(user_id, dateTimeString);


                        String[] strings = ans.split("\n");
                        for (int i = 0; i < strings.length; i++) {

                            if (strings[i].matches("第[一二三四]周：")) {
                                week = strings[i].charAt(1);

                                if (week == '一') {
                                    i++;
                                    level++;
                                    taskService.Insert(user_id, strings[i], level);

                                    continue;
                                }
                                i++;
                                order++;
                                iObjectivesService.insert(user_id, week, strings[i], level, order);
//				System.out.println(strings[i]);
                                continue;
                            }
                            System.out.println(level.toString());
                            order++;
                            iObjectivesService.insert(user_id, week, strings[i], level, order);
                        }
                        int tt = taskService.selectTotal(user_id);
                        for (int i = 1; i <= tt; i++) {
                            int jds = iObjectivesService.countgjd(i, user_id);
                            taskService.updateJD(i, jds, user_id);
                        }
                        return Result.successInfo(ResultType.GET, ans);
                    }


                    return Result.successInfo(ResultType.GET, ans);

                }
            }
        }

        return Result.successInfo(ResultType.GET, "其他问题");
    }
}
